// pages/order/index.js
import {request} from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders:[],
    tabs:[{
      id:1,
      name:"全部",
      isActive: true
    },
    {
      id:2,
      name:"待付款",
      isActive:false,
    },{
      id:3,
      name:"待发货",
      isActive:false
    },{
        id:4,
        name:"退货",
        isActive:false
    }],
  },
  handleTabsIndex(index){
    console.log(index)
    const {tabs}=this.data
    tabs.forEach((v,i)=>{
      i===index?v.isActive=true:v.isActive=false
    })
    this.setData({tabs:tabs})
  },
  handleTabsItemChange(e){
    console.log(e)
    this.handleTabsIndex(e.detail.index);
    this.getOrders(e.detail.index+1);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
     let page=getCurrentPages();
     let currentPage=page[page.length-1];
     let {status}=currentPage.options;
     this.handleTabsIndex(status-1);
     let token=wx.getStorageSync('token');
     if(!token){
       wx.navigateTo({
         url: '/pages/auth/index',
       });
       return;
     }
     this.getOrders(status)
  },
  getOrders(status){
    request({url:"http://localhost:8002/wx/order/"+status,header: { 
      "Content-Type": "application/x-www-form-urlencoded",
       "Authorization":wx.getStorageSync('token')
      }}).then(res=>{
       this.setData({orders:res.data.data.data}) 
      })
  }
})